<template>
  <div class="bill-section">
    <div class="bill-header">
      <div class="header-left">
        <h2>💰 账单详情</h2>
        <span class="bill-badge">房间 {{ bill.roomId }}</span>
      </div>
      <button @click="handlePrint" class="btn-print">
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" style="margin-right: 6px;">
          <path d="M6 9V2h12v7M6 18H4a2 2 0 01-2-2v-5a2 2 0 012-2h16a2 2 0 012 2v5a2 2 0 01-2 2h-2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          <path d="M6 14h12v8H6z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
        导出打印账单
      </button>
    </div>

    <div class="bill-content">
      <!-- 客户信息 -->
      <div v-if="bill.guestName || bill.guestPhone" class="guest-info">
        <h3>👤 客户信息</h3>
        <div class="info-grid">
          <div v-if="bill.guestName" class="info-item">
            <span class="label">客户姓名：</span>
            <span class="value">{{ bill.guestName }}</span>
          </div>
          <div v-if="bill.guestPhone" class="info-item">
            <span class="label">联系电话：</span>
            <span class="value">{{ bill.guestPhone }}</span>
          </div>
        </div>
      </div>

      <!-- 入住信息 -->
      <div class="bill-summary">
        <h3>🏠 入住信息</h3>
        <div class="summary-row">
          <span>房间号：</span>
          <span class="value-bold">{{ bill.roomId }}</span>
        </div>
        <div class="summary-row">
          <span>入住时间：</span>
          <span>{{ formatDateTime(bill.checkInTime) }}</span>
        </div>
        <div class="summary-row">
          <span>退房时间：</span>
          <span>{{ formatDateTime(bill.checkOutTime) }}</span>
        </div>
        <div class="summary-row">
          <span>入住天数：</span>
          <span class="value-bold">{{ bill.stayDays || calculateStayDays() }} 天</span>
        </div>
      </div>

      <!-- 费用明细 -->
      <div class="bill-charges">
        <h3>💰 费用明细</h3>
        
        <!-- 房费 -->
        <div class="charge-section">
          <div class="charge-row">
            <span>房费</span>
            <span>¥{{ (bill.roomRate || 280) }}/天 × {{ bill.stayDays || calculateStayDays() }}天</span>
            <span class="amount">¥{{ (bill.roomCharge || calculateRoomCharge()).toFixed(2) }}</span>
          </div>
        </div>

        <!-- 空调使用费 -->
        <div class="charge-section">
          <div class="charge-row">
            <span>空调使用费</span>
            <span>耗电 {{ bill.totalPowerConsumption.toFixed(3) }} 度 ({{ formatDuration(bill.totalServiceDuration) }})</span>
            <span class="amount">¥{{ bill.totalCost.toFixed(2) }}</span>
          </div>
          <!-- 详单费用总和提示 -->
          <div v-if="getDetailRecordsTotalCost() !== bill.totalCost" class="charge-note">
            <span>⚠️ 注意：详单记录总和 ¥{{ getDetailRecordsTotalCost().toFixed(2) }}，实际费用以系统计算为准</span>
          </div>
        </div>

        <!-- 小计 -->
        <div class="charge-subtotal">
          <span>小计</span>
          <span class="amount">¥{{ calculateSubtotal().toFixed(2) }}</span>
        </div>

        <!-- 押金说明 -->
        <div class="deposit-section">
          <div class="deposit-info">
            <div class="deposit-icon">🔒</div>
            <div class="deposit-content">
              <div class="deposit-title">押金处理说明</div>
              <div class="deposit-desc">
                • 入住时收取押金：¥{{ (bill.deposit || 200).toFixed(2) }}<br>
                • 退房时原路退还，与住宿费用分离计算<br>
                • 押金不包含在应付金额中
              </div>
            </div>
          </div>
        </div>

        <!-- 最终应付金额（不含押金） -->
        <div class="charge-final">
          <div class="final-label">
            <span>应付金额</span>
            <span class="final-note">（不含押金，押金单独退还）</span>
          </div>
          <span class="final-amount">
            ¥{{ calculateSubtotal().toFixed(2) }}
          </span>
        </div>
      </div>

      <!-- 详单 -->
      <div class="detail-records">
        <div class="detail-header">
          <h3>📋 空调使用详单</h3>
          <div class="record-summary">
            <span class="record-count">共 {{ bill.detailRecords.length }} 条记录</span>
            <span class="record-total">详单合计：¥{{ getDetailRecordsTotalCost().toFixed(2) }}</span>
          </div>
        </div>
        <div class="records-table">
          <div class="table-header">
            <span>时间</span>
            <span>操作</span>
            <span>温度</span>
            <span>时长</span>
            <span>耗电 (度)</span>
            <span>费用 (元)</span>
          </div>
          <div v-for="(record, index) in bill.detailRecords" :key="index" class="table-row">
            <span class="time">{{ formatTime(record.timestamp) }}</span>
            <span class="action" :class="getActionClass(record.action)">
              {{ record.action }}
            </span>
            <span class="temp">{{ record.currentTemp.toFixed(1) }}°C</span>
            <span class="duration">{{ formatDuration(record.duration) }}</span>
            <span class="power">{{ record.powerConsumption.toFixed(3) }}</span>
            <span class="cost">¥{{ record.cost.toFixed(2) }}</span>
          </div>
          <div v-if="bill.detailRecords.length === 0" class="empty-records">
            <span>暂无使用记录</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Bill } from '../../types/index';

const props = defineProps<{
  bill: Bill;
}>();

const emit = defineEmits<{
  print: [bill: Bill];
}>();

const handlePrint = () => {
  emit('print', props.bill);
};

const formatDateTime = (timestamp: number): string => {
  return new Date(timestamp).toLocaleString('zh-CN');
};

const formatTime = (timestamp: number): string => {
  return new Date(timestamp).toLocaleTimeString('zh-CN');
};

const formatDuration = (seconds: number): string => {
  if (seconds === 0) return '-';
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = seconds % 60;
  
  const parts = [];
  if (hours > 0) parts.push(`${hours}小时`);
  if (minutes > 0) parts.push(`${minutes}分钟`);
  if (secs > 0) parts.push(`${secs}秒`);
  
  return parts.join('') || '-';
};

const getActionClass = (action: string): string => {
  if (action.includes('开始') || action.includes('开机')) return 'action-start';
  if (action.includes('停止') || action.includes('关机')) return 'action-stop';
  if (action.includes('调') || action.includes('设置')) return 'action-adjust';
  return 'action-normal';
};

// 计算入住天数
const calculateStayDays = (): number => {
  const days = Math.ceil((props.bill.checkOutTime - props.bill.checkInTime) / (1000 * 60 * 60 * 24));
  return days > 0 ? days : 1;
};

// 计算房费
const calculateRoomCharge = (): number => {
  const rate = props.bill.roomRate || 280; // 默认房费 280元/天
  const days = props.bill.stayDays || calculateStayDays();
  return rate * days;
};

// 计算小计
const calculateSubtotal = (): number => {
  return (props.bill.roomCharge || calculateRoomCharge()) + props.bill.totalCost;
};

// 计算详单记录的费用总和
const getDetailRecordsTotalCost = (): number => {
  return props.bill.detailRecords.reduce((sum, record) => sum + record.cost, 0);
};
</script>

<style scoped>
.bill-section {
  margin-bottom: 24px;
  padding: 28px;
  background: white;
  border-radius: 16px;
  border: 3px solid #067ef5;
  box-shadow: 0 8px 24px rgba(6, 126, 245, 0.15);
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.bill-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 3px solid #067ef5;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

h2 {
  margin: 0;
  color: #1e293b;
  font-size: 20px;
  font-weight: 700;
}

.bill-badge {
  padding: 6px 14px;
  background: linear-gradient(135deg, #067ef5 0%, #0284c7 100%);
  color: white;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(6, 126, 245, 0.3);
}

h3 {
  margin: 24px 0 16px 0;
  color: #334155;
  font-size: 16px;
  font-weight: 600;
}

.btn-print {
  padding: 12px 24px;
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
  display: flex;
  align-items: center;
}

.btn-print:hover {
  background: linear-gradient(135deg, #059669 0%, #047857 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}

.bill-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 客户信息 */
.guest-info {
  padding: 20px 24px;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  border-radius: 12px;
  border: 2px solid #bae6fd;
}

.guest-info h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: white;
  border-radius: 8px;
  border: 2px solid #f0f4f8;
}

.info-item .label {
  color: #64748b;
  font-weight: 500;
  font-size: 14px;
}

.info-item .value {
  font-weight: 600;
  color: #1e293b;
  font-size: 15px;
}

/* 入住信息 */
.bill-summary {
  padding: 24px;
  background: #f8fafc;
  border-radius: 12px;
  border: 2px solid #e0f2fe;
}

.bill-summary h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #e2e8f0;
  color: #475569;
  font-size: 15px;
}

.summary-row:last-child {
  border-bottom: none;
}

.value-bold {
  font-weight: 700;
  color: #1e293b;
}

/* 费用明细 */
.bill-charges {
  padding: 24px;
  background: linear-gradient(135deg, #fefce8 0%, #fef3c7 100%);
  border-radius: 12px;
  border: 2px solid #fde047;
}

.bill-charges h3 {
  margin: 0 0 20px 0;
  font-size: 16px;
}

.charge-section {
  margin-bottom: 16px;
}

.charge-row {
  display: grid;
  grid-template-columns: 150px 1fr 120px;
  gap: 16px;
  padding: 14px 20px;
  background: white;
  border-radius: 8px;
  border: 2px solid #fef3c7;
  align-items: center;
}

.charge-row span:first-child {
  font-weight: 600;
  color: #1e293b;
}

.charge-row span:nth-child(2) {
  color: #64748b;
  font-size: 14px;
}

.charge-row .amount {
  text-align: right;
  font-weight: 700;
  color: #1e293b;
  font-size: 16px;
}

.charge-row .amount.refund {
  color: #10b981;
}

.charge-subtotal {
  display: flex;
  justify-content: space-between;
  padding: 16px 20px;
  background: white;
  border-radius: 8px;
  border: 2px solid #fde047;
  font-weight: 600;
  font-size: 16px;
  margin: 20px 0;
}

.charge-subtotal .amount {
  color: #1e293b;
  font-weight: 700;
}

.deposit-section {
  background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
  padding: 20px 24px;
  border-radius: 12px;
  border: 2px solid #93c5fd;
  margin: 20px 0;
}

.deposit-info {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

.deposit-icon {
  font-size: 32px;
  line-height: 1;
}

.deposit-content {
  flex: 1;
}

.deposit-title {
  font-size: 16px;
  font-weight: 700;
  color: #1e40af;
  margin-bottom: 8px;
}

.deposit-desc {
  font-size: 14px;
  color: #1e40af;
  line-height: 1.8;
}

.charge-note {
  margin-top: 8px;
  padding: 8px 12px;
  background: #fef3c7;
  border-left: 4px solid #f59e0b;
  border-radius: 4px;
  font-size: 13px;
  color: #92400e;
}

.charge-final {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 28px;
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  border-radius: 12px;
  margin-top: 24px;
  box-shadow: 0 6px 20px rgba(220, 38, 38, 0.4);
}

.final-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.final-label > span:first-child {
  color: white;
  font-size: 20px;
  font-weight: 700;
}

.final-note {
  color: rgba(255, 255, 255, 0.85);
  font-size: 13px;
  font-weight: 500;
}

.final-amount {
  font-size: 32px;
  font-weight: 900;
  color: #fde047;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.record-summary {
  display: flex;
  gap: 16px;
  align-items: center;
}

.record-total {
  padding: 6px 14px;
  background: white;
  color: #dc2626;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 700;
  border: 2px solid #fca5a5;
}

.detail-records {
  padding: 24px;
  background: #f8fafc;
  border-radius: 12px;
  border: 2px solid #e0f2fe;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.record-count {
  padding: 4px 12px;
  background: #e0f2fe;
  color: #0369a1;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
}

.records-table {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}

.table-header,
.table-row {
  display: grid;
  grid-template-columns: 80px 150px 70px 90px 70px 70px;
  gap: 12px;
  padding: 12px 16px;
  background: white;
  border-radius: 8px;
}

.table-header {
  font-weight: 600;
  background: #067ef5;
  color: white;
  border: 2px solid #067ef5;
}

.table-row {
  border: 2px solid #f0f4f8;
  transition: all 0.2s;
}

.table-row:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
}

.table-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #475569;
}

.action {
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 6px;
}

.action-start {
  background: #d1fae5;
  color: #065f46;
}

.action-stop {
  background: #fee2e2;
  color: #991b1b;
}

.action-adjust {
  background: #dbeafe;
  color: #1e40af;
}

.action-normal {
  background: #f3f4f6;
  color: #374151;
}

.empty-records {
  padding: 40px;
  text-align: center;
  color: #94a3b8;
  font-size: 15px;
  background: white;
  border-radius: 8px;
  border: 2px dashed #e2e8f0;
}
</style>

